﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xaml;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.ObjectModel;


namespace ConsoleApplication1
{
	static class Program
	{
		static void Main(string[] args)
		{
			//var schema = new XamlSchemaContext();
			//var writer = new XamlObjectWriter(schema);



			var x1 = ParseXaml(File.ReadAllText("XAMLFile1.xaml"));
			//var z = System.Windows.Markup.XamlReader.Parse(File.ReadAllText("XAMLFile1.xaml"));
			var s = CreateXaml(x1);


			//var x2 = ParseXamlUsingSX(File.ReadAllText("XAMLFile2.xaml"));

		}


		public static object ParseXaml(string xamlString)
		{
			using (var reader = new XamlXmlReader(XmlReader.Create(new StringReader(xamlString))))
			using (var writer = new XamlObjectWriter(reader.SchemaContext))
			{
				while (reader.Read())
				{
					writer.WriteNode(reader);
				}
				return writer.Result;
			}
		}


		public static string CreateXaml(object source)
		{
			using (var reader = new XamlObjectReader(source))
			using (var xamlString = new StringWriter())
			using (var writer = new XamlXmlWriter(xamlString, reader.SchemaContext))
			{
				while (reader.Read())
				{
					writer.WriteNode(reader);
				}
				return xamlString.ToString();
			}
		}

	}


	[System.Windows.Markup.ContentProperty("Content")]
	public class Root
	{
		public Root()
		{
			Content = new QuickList();
		}

		public Root(string initialContent)
			: this()
		{
			Content.Add(initialContent);
		}
		public Root(params string[] initialContent)
			: this()
		{
			foreach (var item in initialContent)
			{
				Content.Add(item);
			}
		}
		//private readonly ObservableCollection<object> m_Content = new ObservableCollection<object>();
		//
		//public ObservableCollection<object> Content
		//{
		//    get { return m_Content; }
		//}

		//public object Content { get; set; }
		public QuickList Content { get; private set; }

	}

	public class QuickList : ObservableCollection<object>
	{
		protected override void InsertItem(int index, object item)
		{
			base.InsertItem(index, item);
		}
	}
}
